//
//  KVConstemTabbar.swift
//  KVBestMicroblog
//
//  Created by 李康卫 on 16/7/18.
//  Copyright © 2016年 李康卫. All rights reserved.
//

import UIKit

class KVConstemTabbar: UITabBar {
    
    // MARK: - 属性
    var selectBackBlock:(() -> Void)?

    //item的数量
    let itemNumb: Int = 5
 // 从新布局tabBaritem的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //添加按钮的点击事件
        self.addSubview(btn)
        //item的宽度
        let itemW = self.frame.width / CGFloat(itemNumb)
        let itemH = self.frame.height
        var index = 0
        //拿到所有的SubView
        for tBItme in self.subviews {
            
            //UITabBarButton过滤掉其他类型
            if tBItme.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                let itemX:CGFloat = itemW*CGFloat(index)
                
                tBItme.frame = CGRectMake(itemX, 0, itemW, itemH)
                index++
                
                //如果是第2个就设置为Button
                if index == 2 {
                    //添加自定义的ButtonFarme
                    btn.frame = CGRectMake(itemX + itemW, 0, itemW, itemH)
                    index++
                }
            }
        }

    }
    // MARK: - 私有方法
    func selectAdd() {
        //闭包回调
        selectBackBlock?()
        
    }

    // MARK:- ---- 懒加载
    lazy var btn: UIButton = {
        
        var button = UIButton(type: UIButtonType.Custom)
        //设置按钮的属性
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //添加Butto
        button.addTarget(self, action: "selectAdd", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

}
